/*
 * Copyright 2018 Zhongan.com All right reserved. This software is the
 * confidential and proprietary information of Zhongan.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Zhongan.com.
 */

package com.example.cloud.service;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 测试文件流
 *
 * @author tongyufu
 * @since 2018年7月6日 上午10:29:02
 */
@RestController
@RequestMapping("/file")
public class FileService {

    /**
     * 上传
     *
     * @param file 文件
     * @param request 请求
     * @return success
     * @throws IOException IOException
     */
    @RequestMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        System.out.println("====" + file.isEmpty());
        System.out.println("----" + request.getInputStream().toString());
        return "success";
    }

    /**
     * 下载
     *
     * @param response 响应
     * @throws IOException IOException
     */
    @RequestMapping("/download")
    public void download(HttpServletResponse response) throws IOException {
        InputStream inputStream = FileService.class.getResourceAsStream("/application.yml");
        response.setHeader("Content-Disposition", "attachment; filename=application.yml");
        response.setContentType("application/octet-stream;charset=UTF-8");
        FileCopyUtils.copy(inputStream, response.getOutputStream());
    }
}
